#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
 basic.py
@author: lyndon
@time Created on 2018/12/28 0:51
@desc 再次重复说明，下面代码只是为了简单说明接口获取方式。
实际中并不推荐，尤其是业务繁重的公众号，更需要中控服务器，统一的获取accessToken。
"""

import urllib
import time
import json
from urllib2 import Request, urlopen

class Basic:
    def __init__(self):
        self.__accessToken = ''
        self.__leftTime = 0

    def __real_get_access_token(self):
        '''
        获取Token
        :return:
        '''
        appId = "wxb7f6f86dfbfadcda"
        appSecret = "c1ef7417cc42c1cf5ab3bff00fc3d505"
        postUrl = ("https://api.weixin.qq.com/cgi-bin/token?grant_type="
                   "client_credential&appid=%s&secret=%s" % (appId, appSecret))
        urlResp = urllib.urlopen(postUrl)
        urlResp = json.loads(urlResp.read())
        self.__accessToken = urlResp['access_token']
        self.__leftTime = urlResp['expires_in']

    def get_access_token(self):
        if self.__leftTime < 10:
            self.__real_get_access_token()
            return self.__accessToken

    def get_left_time(self):
        return self.__leftTime

    def run(self):
        while True:
            if self.__leftTime > 10:
                time.sleep(2)
                self.__leftTime -= 2
            else:
                self.__real_get_access_token()

    def set_menu(self, token='19_YY8tJsx9QtScFIkZAArIjUUPdLjLssEmoRzzyhulQ7CSj_2jcM7K12FDuTnPRWDovXO_PPqalsD4a0KjkOoQTtTeaxM6aAoIhipCws4RlwTjtyaYeqJW6bQXTMW3F7OMWQgp3MGa56xierXFOXEfAIAVEL'):

        menu = {
            "button": [
                {
                    "type": "click",
                    "name": "今日博文",
                    "key": "V1001_TODAY_BLOG"
                },
                {
                    "type": "click",
                    "name": "资源领取",
                    "key": "V1002_RES_GET"
                },
                {
                    "name": "关于",
                    "sub_button": [
                        {
                            "type": "view",
                            "name": "搜索",
                            "url": "http://www.soso.com/"
                        },
                        {
                            "type": "click",
                            "name": "赞一下我们",
                            "key": "V1001_GOOD"
                        }]
                }]
        }

        test_menu_urlencode = urllib.urlencode(menu)
        print(test_menu_urlencode)
        header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',
                       "Content-Type": "application/json"}
        url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s' % (token,)
        req = Request(url=url, data=test_menu_urlencode, headers=header_dict)
        res = urlopen(req)
        res = res.read()
        print(res)




if __name__ == '__main__':
    b = Basic()
    # accressToken = b.get_access_token()
    # print(accressToken)
    # leftTime = b.get_left_time()
    # print(leftTime)
    b.set_menu()
